
console.info("success")
var upWindow = window.parent.frames["zwIframe"];

function fy_dialog_close() {
//	window.parent.$(".mask").remove();
//	window.parent.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");
    window.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");

}

function getUpFields(name) {
    return upWindow.$("[validate*='name:\"" + name + "\"']");
}
function getUpFieldsByFieldval(name) {
    return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
}

function $(name) {
    return document.getElementsByName(name);
}

function getUpAccountFieldValue(name) {
    var field = getUpFields(name).eq(0);
    return {
        displayValue: field.is("span") ? field.text() : field.val(),
        value: eval("(" + field.parent("span").attr("fieldVal") + ")").value
    };
}

function getSpanFieldValue(name) {
    var field = getUpFieldsByFieldval(name).eq(0);
    return eval("(" + field.attr("fieldVal") + ")").value;
}

function setUpFieldValue(name, value) {
    upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input").val(
            value);
    upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input")
            .trigger("blur");
}

function setUpFieldValue2(name, value) {
    try {
        var field = getUpFieldsByFieldval(name).eq(0);
        var fieldVal = field.attr("fieldVal");

        if (fieldVal != null && typeof (fieldVal) != 'undefined') {
            fieldVal = eval("(" + fieldVal + ")");
        } else {
            return null;
        }
        var inField = field.find("#" + fieldVal.name);
        inField.val(value);
        inField.trigger("blur");
    } catch (e) {
    }
}

function selectValueChange(dom) {
    if (dom.value == 0) {
        document.getElementById("queryCostDIV").style.display = "";
        document.getElementById("showCostDIV").style.display = "";
        document.getElementById("fillCostDIV").style.display = "none";
    } else if (dom.value == 1) {
        document.getElementById("fillCostDIV").style.display = "";
        document.getElementById("queryCostDIV").style.display = "none";
        document.getElementById("costDIV").style.display = "none";
        document.getElementById("showCostDIV").style.display = "none";
    }
}
function repalceHTMLStr(str) {
    return !str ? "" : str.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(
            /'/g, "\\'");
}

function fillSum(checkBox) {
    if (checkBox.checked) {
        $("sumCost")[0].style.display = "";
    } else {
        $("sumCost")[0].style.display = "none";
    }
}

function checkCost(costValue) {
    var costStr = ([(costValue)].join(''));
    if (costValue.replace(/ /g, '') == "") {
        alert("金额不能为空。")
    } else if (costStr.indexOf('.') == 0 || isNaN(costValue)) {
        alert("金额必须为数字值。")
    } else {
        if (costStr.indexOf('.') != -1
                && (costStr.length - costStr.indexOf('.') > 3)) {
            alert("金额只保留小数点后两位。");
        } else {
            return true;
        }
    }
    return false;
}

function fillCost() {
    if (checkCost($("cost")[0].value) && checkCost($("cost_year")[0].value) && checkCost($("sumCost1")[0].value)) {
        setUpFieldValue2(getUpFieldsByFieldval("预计金额输入项名称").eq(0).find("span")
                .text() + "1", $("cost_year")[0].value);

        setUpFieldValue2("已审批金额合计1", $("sumCost1")[0].value);
        setUpFieldValue2("预计费用合计", $("cost")[0].value);
        if (getUpFieldsByFieldval("预算编号1").length > 0) {
            setUpFieldValue2("预算编号1", '无');
        }
        var remainder = accSub($("cost_year")[0].value, $("cost")[0].value);
        if (getUpFieldsByFieldval("年度预算剩余金额1").length > 0) {
            setUpFieldValue2("年度预算剩余金额1", remainder >= 0 ? remainder : 0);
        }
        fy_dialog_close();
    } else
        return false;
}

function accSub(arg1, arg2) {
    var r1, r2, m, n;
    try {
        r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2)); // last modify by deeka //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

function onSelectCost(dom) {
    if ($("cost_other")[0].value.replace(/ /g, '') == "") {
        alert("预计费用合计不能为空");
        return;
    }
    if (!checkCost($("cost_other")[0].value)) {
        return;
    }

    if (!dom) {
        dom = this;
    }
    var inputNodes = dom.parentNode.getElementsByTagName("input");
    var number;
    var cost_year;
    if (inputNodes[0].name == 'numberInput') {
        number = inputNodes[0].value;
        cost_year = inputNodes[1].value;
    } else {
        number = inputNodes[1].value;
        cost_year = inputNodes[0].value;
    }
    if ($("fillSum")[0].checked) {
        if ($("sumCost")[0].value.replace(/ /g, '') == "") {
            alert("已审批金额合计不能为空");
            return;
        }
        if (!checkCost($("sumCost")[0].value)) {
            return;
        }
        setUpFieldValue2("预算编号1", number);
        setUpFieldValue2(getUpFieldsByFieldval("预计金额输入项名称").find("span")
                .text() + 1, cost_year);
        setUpFieldValue2("已审批金额合计1", $("sumCost")[0].value);
        setUpFieldValue2("预计费用合计", $("cost_other")[0].value);
        var remainder = accSub(accSub(cost_year, $("sumCost")[0].value),
                $("cost_other")[0].value);
        if (getUpFieldsByFieldval("年度预算剩余金额1").length > 0)
            setUpFieldValue2("年度预算剩余金额1", remainder >= 0 ? remainder : 0);
        setUpFieldValue2("合计立项金额截止时间1", "");
        fy_dialog_close();
    } else {

        _ajax("../common.do?method=queryYSBHCost&operate=1&number=" + number,
                function (text) {
                    var result = eval("(" + text + ")");

                    if (result.success) {
                        console.log(result.data);
                        setUpFieldValue2("预算编号1", number);
                        setUpFieldValue2(getUpFieldsByFieldval("预计金额输入项名称")
                                .find("span").text() + 1, cost_year);
                        setUpFieldValue2("合计立项金额截止时间1", result.data.time);
                        setUpFieldValue2("已审批金额合计1", result.data.cost);
                        setUpFieldValue2("预计费用合计", $("cost_other")[0].value);
                        var remainder = accSub(accSub(cost_year,
                                result.data.cost), $("cost_other")[0].value);
                        if (getUpFieldsByFieldval("年度预算剩余金额1").length > 0)
                            setUpFieldValue2("年度预算剩余金额1",
                                    remainder >= 0 ? remainder : 0);
                        fy_dialog_close();
                    } else {
                        alert(result.msg);
                    }
                });
    }
}

function showCost(data) {
    if (data && data.length > 0) {
        var browser = navigator.appName;
        var version = navigator.appVersion;

        var tableHTML = [
            "<table class='showTable' align='center'>",
            "<tr><td class='number'>预算编号</td><td class='subject'>投资项目名称</td><td class='dept'>部门</td><td class='childDept'>车间/科室</td><td class='cost'>年度预算金额(万元)</td><td></td></tr>"]
        for (var i = 0; i < data.length; i++) {
            tableHTML.push("<tr>");
            tableHTML.push("<td  class='number'>");
            tableHTML.push(data[i].number);
            tableHTML.push("</td>");
            tableHTML.push("<td  class='subject'>");
            tableHTML.push(data[i].name);
            tableHTML.push("</td>");
            tableHTML.push("<td  class='dept'>");
            tableHTML.push(data[i].dept);
            tableHTML.push("</td>");
            tableHTML.push("<td  class='childDept'>");
            tableHTML.push(data[i].childDept ? data[i].childDept : "");
            tableHTML.push("</td>")
            tableHTML.push("<td  class='cost'>");
            tableHTML.push(data[i].cost);
            tableHTML.push("</td>");
            tableHTML.push("<td>");
            tableHTML.push("<a onclick='onSelectCost(this)'>选择</a>");
            tableHTML.push("<input class='hide' name='numberInput' value='");
            tableHTML.push(repalceHTMLStr(data[i].number));
            tableHTML.push("'/>");
            tableHTML.push("<input class='hide' name='numberInput' value='");
            tableHTML.push(repalceHTMLStr(data[i].cost));
            tableHTML.push("'/>");
            tableHTML.push("</td>");
            tableHTML.push("</tr>");
        }
        tableHTML.push("</table>");
        document.getElementById("showCostDIV").innerHTML = tableHTML.join('');
        document.getElementById("costDIV").style.display = "";
    } else {
        alert("未查找到投资项目预算金额。");
    }
}

function queryCost() {
    $("number")[0].value = $("number")[0].value.replace(/ /g, '');
    $("subject")[0].value = $("subject")[0].value.replace(/ /g, '');
    document.getElementById("showCostDIV").innerHTML = "";
    _ajax("../common.do?method=queryYSBHCost&number=" + $("number")[0].value
            + "&name=" + encodeURIComponent($("subject")[0].value), function (
            text) {
        var result = eval("(" + text + ")");
        if (result.success) {
            showCost(result.data);
        } else {
            alert(result.msg);
        }
    });
    return false;
}

function _ajax(url, operate) {
    var xmlhttp = null;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
        xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                text = xmlhttp.responseText;
                operate(text);
            } else {
                alert("连接服务器异常");
            }
        }
    };
    xmlhttp.open("get", url + "&temp=" + Math.random(), true);
    xmlhttp.send();
}
window.onload = function () {
    if (getUpFieldsByFieldval("预算编号").length == 0) {
        document.getElementById("selectDIV").style.display = "none";
    }
}